package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.AddressQuery;
import cn.org.eshow.model.Address;
import cn.org.eshow.model.User;
import cn.org.eshow.service.AddressManager;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static cn.org.eshow.webapp.util.RenderUtil.failure;
import static cn.org.eshow.webapp.util.RenderUtil.success;

/**
 * 地址API接口
 */
@AllowedMethods({"mine", "save","update", "delete"})
public class AddressAction extends ApiBaseAction<Address> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private AddressManager addressManager;

    private Address address = new Address();
    private List<Address> addresses = new ArrayList<Address>();
    private AddressQuery query = new AddressQuery(Boolean.TRUE);

    /**
     * 我的地址列表
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        addresses = addressManager.list(query);
        if (addresses.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        RenderUtil.list("获取成功", "addresses", addresses);
    }

    /**
     * 添加地址信息
     */
    public void save() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        address = addressManager.save(address, user);
        success("添加成功");
    }

    /**
     * 修改地址
     */
    public void update() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        Address old = addressManager.get(id);
        if (old == null) {
            failure("非法参数");
            return;
        }
        addressManager.update(old, address, user);
        success("更新成功");
    }

    /**
     * 删除地址信息
     */
    public void delete() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        Address old = addressManager.get(id);
        if (old == null) {
            failure("非法参数");
            return;
        }
        addressManager.enabled(old);
        success("删除成功");
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public AddressQuery getQuery() {
        return query;
    }

    public void setQuery(AddressQuery query) {
        this.query = query;
    }

}